package com.airforcehospital;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

@Slf4j
public class AirHttpTool2 {

    public static void main(String[] args) {
        JSONObject res = submit("09:00","09:30","50913244","1318754","皮肤科门诊");
        System.out.println(res);
    }
    // 改id  改签名
    private  static  String url = "https://m.hsyuntai.com/med/hp/hospitals/100578/registration/submit230";
    //14591
    private static String entityStr = "{\"patId\":33018182,\"patName\":\"郝堙柳\",\"pcId\":29194412,\"schId\":955760635167,\"isSendRegSms\":\"Y\",\"sTime\":\"stime\",\"eTime\":\"etime\",\"type\":\"0\",\"regSelectItems\":{\"ChargeType\":\"公费\"},\"vcode\":\"\",\"docId\":\"docId\",\"hosDistId\":\"236\",\"deptId\":\"deptId\",\"districtId\":\"236\",\"patCardType\":6,\"patCardName\":\"就诊ID号\",\"deptName\":\"deptName\",\"isHealthCard\":false,\"timeout\":30000,\"isLoading\":true}";

    public static JSONObject submit(String sTime,String eTime,String docId,String deptId,String deptName)  {

        entityStr.replace("stime",sTime).replace("etime",eTime).replace("docId",docId).replace("deptId",deptId).replace("deptName",deptName);
        return post();

    }

    public static JSONObject post()  {

        CloseableHttpResponse response ;
        JSONObject result=null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(50000) // 连接超时时间，单位毫秒
                .setSocketTimeout(50000)  // 读取超时时间，单位毫秒
                .build();
        post.setConfig(requestConfig);
        post.setHeader("Referer","https://m.hsyuntai.com/med/hp/hospitals/100578/hos/registration/orderDetails/0/236/1318754/50913244?deptName=%E7%9A%AE%E8%82%A4%E7%A7%91%E9%97%A8%E8%AF%8A");
        post.setHeader("Accept-Encoding","gzip, deflate, br");
        post.setHeader("accept","application/json, text/plain, */*");
        post.setHeader("yt-baseinfo","%7B%22openid%22%3A%22ofsEA1r97u0g6MTjykbZg-MezWfY%22%2C%22thirdAppId%22%3A%22wx1d99f89bd913773e%22%2C%22access_token%22%3Atrue%2C%22appId%22%3A%22100578%22%7D");
        post.setHeader("openid","ofsEA1r97u0g6MTjykbZg-MezWfY");
        post.setHeader("Accept-Language","zh-CN,zh;q=0.9");
        post.setHeader("x-requested-with","XMLHttpRequest");
        post.setHeader("tt_upload_params","[object Object]");
        post.setHeader("ngversion","99.2.2");
        post.setHeader("content-type","application/json;charset=UTF-8");
        post.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 NetType/WIFI MicroMessenger/7.0.20.1781(0x6700143B) WindowsWechat(0x63090c11) XWEB/11275 Flue");
        post.setHeader("Sec-Fetch-Dest","empty");
        post.setHeader("unicode","5CS1SGlrinAJQbnnnnBCPFZufdXZPI");
        post.setHeader("Host","m.hsyuntai.com");
        post.setHeader("Sec-Fetch-Mode","cors");
        post.setHeader("Sec-Fetch-Site","same-origin");
        post.setHeader("Connection","keep-alive");
        post.setHeader("Origin","https://m.hsyuntai.com");
        post.setHeader("Cookie","acw_tc=0a47308217325850503182382e004114a1872b59a29e84b973a691aad54a95; connect.sid=s%3Al3VUmwhIkhuuw8L27BnpTl11dK5PPpgN.R4xeKdyYw%2B3cI%2FdEQHzn%2F%2FDgkohndYwOEQuOdDfou8Q");
        post.setEntity(new StringEntity(entityStr, ContentType.APPLICATION_JSON));


        try{
            response = httpclient.execute(post);
            String body = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
            result = JSONObject.parseObject(body);
            //System.out.println(result);
            log.info("抢票结果"+result);

        }catch (Exception e){
            log.error("请求接口出错"+e.toString());
            e.printStackTrace();
        }


        return result;
    }
}
